package com.yksj.healthtalk.ui.doctorstation;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.CommonExplainActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;

/**
 * 网上咨询
 */
public class DoctorMarkTalkFragment extends BaseFragmentActivity implements
		OnClickListener, OnCheckedChangeListener {

	private TextView mContent;
	private String SERVICE_TYPE_ID;
	private ToggleButton mButton;
	private boolean isUpdate=false;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_mark_talk_fragment);
		initTitle();
		initView();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		
		SERVICE_TYPE_ID = getIntent().getStringExtra("SERVICE_TYPE_ID");
		titleTextV.setText(getIntent().getStringExtra("SERVICE_TYPE_NAME"));
		mButton = (ToggleButton) findViewById(R.id.sound_open);
		
		findViewById(R.id.doctor_choice_service_time).setOnClickListener(this);
		findViewById(R.id.doctor_line_content).setOnClickListener(this);
		
		mContent = (TextView)findViewById(R.id.docotr_content);
//		mContent.setOnClickListener(this);
		TextView changeText=(TextView)findViewById(R.id.change_text);
		
		if("3".equals(SERVICE_TYPE_ID)){
		changeText.setText(getString(R.string.mint_service));//预约门诊
		}else{
		changeText.setText(getString(R.string.time_service));//网上咨询
		}
		
		initData();
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent =new Intent(this,CoreService.class);
		intent.setAction(CoreService.ACTION_FRAME_VIEW_VISIABLE);
		if("3".equals(SERVICE_TYPE_ID)){//预约门诊
		intent.putExtra("className",this.getClass().getName()+"_1");
		}else{//网上咨询
		intent.putExtra("className",this.getClass().getName()+"_2");	
		}
		startService(intent);
	}
	private void initData() {
		HttpRestClient.doHttpQueryMarkTalkinita(SERVICE_TYPE_ID,new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, String content) {
					super.onSuccess(statusCode, content);
					try {
						JSONObject object=new JSONObject(content);
						if(object.has("error_message")){
							ToastUtil.showBasicShortToast(getApplicationContext(), object.getString("error_message")+"");
						}else{
							//SERVICE_ITEM_ID 医生服务项目ID ORDER_ON_OFF 开启标记: 1-开；0-关
							//SERVICE_ITEM_DESC 医生服务类型自定义说明
							int offORon=object.optInt("ORDER_ON_OFF", 0);
							mButton.setChecked(1==offORon);
							mButton.setOnCheckedChangeListener(DoctorMarkTalkFragment.this);
							mContent.setText(""+object.optString("SERVICE_ITEM_DESC"));
							int count=object.optInt("SELECTCOUNT", 0);
							TextView status= (TextView) findViewById(R.id.status);
							if(count>0){
								status.setText("已选择");
							}else{
								status.setText("未选择");
							}
						}
					} catch (JSONException e) {
					}}}
		);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			if(isUpdate){
				Intent intent = getIntent();
				intent.putExtra("isUpdate",true);
				setResult(RESULT_OK, intent);
			}
			finish();
			break;
		case R.id.doctor_choice_service_time:// 选择时间
			{
				if(!mButton.isChecked()){
					ToastUtil.showToastPanl("请开通服务");
				}else{
					Intent intent =new Intent(this,DoctorServiceTimeListActivity.class);
					intent.putExtra("SERVICE_TYPE_ID", SERVICE_TYPE_ID);
					startActivityForResult(intent, 2000);
				}
			}
			break;
		case R.id.doctor_line_content:// 医生说明
			Intent intent = new Intent(this, CommonExplainActivity.class);
			intent.putExtra("title", "医生提示");  
			intent.putExtra("mContentCount",200);  //字数限制  默认是200
			intent.putExtra("content",  mContent.getText().toString());  //内容
			startActivityForResult(intent, 1000);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1000:
			if(resultCode==Activity.RESULT_OK&&data!=null&&data.hasExtra("content")){
				String contentText=data.getStringExtra("content");
				mContent.setText(contentText);
				submitFeedback(contentText);
			}
			break;
		case 2000:
			initData();
			break;
		}
	}
	
	/**
	 * 提交意见内容
	 */
	private void submitFeedback(String str){
		HttpRestClient.doHttpEditExplain(SERVICE_TYPE_ID,str,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message"));
					}
				} catch (Exception e) {}
			}
		});
	}

	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// 开通服务button
		HttpRestClient.doHttpUpdateYUYUE(SERVICE_TYPE_ID,isChecked,new AsyncHttpResponseHandler(this){
			
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_code")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message","")+"");
					}else{
						isUpdate=true;
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), object.optString("message", "修改成功"));
					}
				} catch (JSONException e) {
				}
			}
		});
	}

}
